package com.liao.ticket.controller;

import com.alibaba.fastjson.JSON;
import com.liao.ticket.service.OrderService;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.params.OrderParams;
import com.liao.ticket.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户验证成功才能操作
 * 每次查询都 自动带上 用户id
 * 接口测试成功
 * 1.0.0
 */
@RestController
@RequestMapping("order")
public class OrderQueryController {
    @Autowired
    private OrderService orderService;

    // 通过 订单id 查询 订单
    @GetMapping("one/{id}")
    public Result selectOne(@PathVariable Long id){
        Result result = orderService.findOrderById(id);
        return result;
    }

    // 通过 分页 查询 订单
    @GetMapping("list")
    public Result selectList(@RequestBody PageParams pageParams) {
        Result result = orderService.findOrderList(pageParams);
        return result;
    }

    // 通过 日期区间 查询 订单
    @GetMapping("list/date")
    public Result selectListByDate(@RequestBody Map map) {
        Object pageParamsJson = map.get("pageParams");
        Object orderParamsJson = map.get("orderParams");
        PageParams pageParams = new PageParams();
        OrderParams orderParams = new OrderParams();
        if(pageParams != null){
            pageParams = JSON.parseObject(pageParamsJson.toString(), PageParams.class);
        }
        if(orderParams != null){
            orderParams = JSON.parseObject(orderParamsJson.toString(), OrderParams.class);
        }
        Result result = orderService.findOrderListByDate(orderParams, pageParams);
        return result;
    }

}
